
#include "cannonball.h"
#include "resourcemanager.h"
#include "gamemanager.h"
#include "frameanimation.h"
#include "gamescene.h"

#define __super         Bullet

static const float ARROW_BULLET_SPEED = 600.0f;

CannonBall::CannonBall(const CCPoint& from, const CCPoint& to, EquipmentAttribute* attr) :
__super(attr), velocity(ccp(ARROW_BULLET_SPEED, 0)) {
	src = from;
	dst = to;
	position = src;

	if(src.x > dst.x) {
		velocity.x = -velocity.x;
	}

	velocity.y = 0.5f * GRAVITY * static_cast<float>(fabs(dst.x - src.x)) / static_cast<float>(fabs(velocity.x));
}

CannonBall::~CannonBall() {
}

void CannonBall::init() {
	__super::init();

	sprite = MAKE_SPRITE("effects/cannonball.png");
	sprite->setZOrder(999);
	sprite->setPosition(position);
	sprite->setScale(GM->minscale * 0.45f);
}

bool CannonBall::update(float ticks) {
	bool ret = true;

	position.x += velocity.x * ticks;
	position.y += velocity.y * ticks;
	velocity.y -= GRAVITY * ticks;

	sprite->setPosition(position);

	if((src.x <= dst.x && position.x >= dst.x) || (src.x >= dst.x && position.x <= dst.x)) {
		FrameAnimation *fxHit = RM->createAnimation("fx_fire_bomb_hit", dst);
		fxHit->setLoop(false);
		fxHit->setAnchorPoint(ccp(0.5f, 0.0f));
		fxHit->setZOrder(static_cast<int>(GM->screenSize.height - dst.y));

		ManagedGameNodeInfo _managedGameNodeInfo;
		_managedGameNodeInfo.node = fxHit;
		GM->gameScene->addOneTimeNode(_managedGameNodeInfo);

		ret = false;
	}

	return ret;
}
